package com.itboye.ihomebank.home;

import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.AvatarDataBeans;
import com.itboye.ihomebank.bean.HeTongBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.presenter.HousePresenter;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.qianyue.OrderFragment;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.HuaBanView;
import com.itboye.ihomebank.util.SPUtils;
import com.itboye.ihomebank.util.SaveViewUtil;
import com.itboye.ihomebank.util.UploadImage;
import com.itboye.ihomebank.util.UploadImage.OnUploadlistener;

public class ActivityHuaBan extends BaseOtherActivity implements Observer {
	//
	HuaBanView huaban;
	String fileName = "";
	ImageView close_icon;
	TextView add_shap_title_tv;
	Button ok, reset;
	private UserPresenter userPresenter;
	private String contractNo;
	private String jsonStr;
	private String uid;
	private List<HeTongBean> datas;
	protected String imgId;
	public static File rootDir = new File(
			Environment.getExternalStorageDirectory() + File.separator
					+ "huaban/");
	private Calendar current = Calendar.getInstance(Locale.CHINA);
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.close_icon:
			finish();
			break;
		case R.id.reset:
			huaban.clearScreen();
			break;
		case R.id.ok:
			showProgressDialog("签名上传中,请稍后", false);
			Long currentMil = System.currentTimeMillis();
			if (SaveViewUtil.saveScreen(huaban, currentMil)) {
				UploadImage.getInstance().execute(
						new String[] { rootDir.getAbsolutePath() + "/"
								+ currentMil + ".jpg" }, "signature",
						new OnUploadlistener() {

							@Override
							public void OnSuccess(
									ArrayList<AvatarDataBeans> resultData) {
								// TODO Auto-generated method stub
								try {

									closeProgressDialog();
								} catch (Exception e) {
									// TODO: handle exception
								}
								imgId = resultData.get(0).getId();
								ByAlert.alert("签名已上传");
								if (contractNo != null) {
									buildJson();// 生成json数据
								} else {
									Intent intent = new Intent();
									intent.putExtra("imgId", imgId);
									setResult(108, intent);
									finish();
								}

							}

							@Override
							public void OnError(String resultData) {
								// TODO Auto-generated method stub
								ByAlert.alert(resultData);
							}
						}, null);
			} else {
				Toast.makeText(this, "保存失败，请检查sdcard是否可用", 0).show();
			}
			break;
		default:
			break;
		}
	}

	protected void buildJson() {
		// TODO Auto-generated method stub
		if (contractNo != null) {
			if (datas == null) {
				ByAlert.alert("合同参数有误");
			} else {
				HashMap<String, String> hashMap = new HashMap<>();
				for (Iterator iterator = datas.iterator(); iterator.hasNext();) {
					HeTongBean heTongBean = (HeTongBean) iterator.next();
					if (heTongBean.getKey().equals("sign_renter_date")) {
						hashMap.put(heTongBean.getKey(), current.get(Calendar.YEAR)+"-"+(current.get(Calendar.MONTH) + 1)+"-"+current.get(Calendar.DATE)+"");
					} else if (heTongBean.getKey().equals("phone_renter")) {
						hashMap.put(heTongBean.getKey(),
								heTongBean.get_default());
					} else if (heTongBean.getKey().equals("sign_renter")) {
						hashMap.put(heTongBean.getKey(), imgId);
					}
				}
				Gson gson=new Gson();
				jsonStr=gson.toJson(hashMap);
				uid = SPUtils.get(this, null, SPContants.USER_ID, "") + "";
				userPresenter.tiJiaoHeTong(uid, contractNo, jsonStr);
			}
		}
	}

	@Override
	protected int layout_id() {
		// TODO Auto-generated method stub
		return R.layout.activity_huabanqianming;
	}

	@Override
	protected void onInitController() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onInit() {
		// TODO Auto-generated method stub
		add_shap_title_tv.setText("填写签名");
		contractNo = getIntent().getStringExtra("contractNo");
		if (contractNo != null) {// 租客填写
			userPresenter = new UserPresenter(this);
			userPresenter.heTongParams("1", contractNo);
		}
	}

	@Override
	protected void onReadIntent(Bundle bundle) {
		// TODO Auto-generated method stub

	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == UserPresenter.heTongParams_success) {
				datas = (List<HeTongBean>) entity.getData();

			} else if (entity.getEventType() == UserPresenter.heTongParams_fail) {

			} else if (entity.getEventType() == UserPresenter.heTongTiJiao_success) {
				Intent intent = new Intent();
				intent.putExtra("shenfen", "renter");
				intent.putExtra("contractNo", contractNo);
				sendBroadcast(intent);
				OrderFragment.getInstance().refresh();
				ByAlert.alert(entity.getData());
				finish();
			} else if (entity.getEventType() == UserPresenter.heTongTiJiao_fail) {
				ByAlert.alert(entity.getData());
			}
		}
	}

}
